package com.g2room.gis;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 计算坐标距离
 */
public class Distance {
    public static void main(String[] args) {
        {
            GlobalCoordinates source = new GlobalCoordinates(106.482769, 29.590643);
//            GlobalCoordinates target = new GlobalCoordinates(30.537422, 104.0562760);
//            double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
//            double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
//            System.out.println("Sphere坐标系计算结果：" + meter1 + "米");
//            System.out.println("WGS84坐标系计算结果：" + meter2 + "米");
            List<Double> doubleLists = getDistanceMeter(source, createGlobalCoordinates(), Ellipsoid.Sphere);
            System.out.println(doubleLists);
        }
    }

    private static List<GlobalCoordinates> createGlobalCoordinates() {
        List<GlobalCoordinates> globalCoordinatesList = new ArrayList<>();
        globalCoordinatesList.add(new GlobalCoordinates(106.482769, 29.590653));
        globalCoordinatesList.add(new GlobalCoordinates(106.48138, 29.590302));
        globalCoordinatesList.add(new GlobalCoordinates(106.478826, 29.590825));
        globalCoordinatesList.add(new GlobalCoordinates(106.477705, 29.591385));
        globalCoordinatesList.add(new GlobalCoordinates(106.474207, 29.591823));
        globalCoordinatesList.add(new GlobalCoordinates(106.478724, 29.590517));
        IntStream.range(0, 999).forEach(i -> globalCoordinatesList.add(new GlobalCoordinates(106.478724 + i / 1000, 29.590517)));
        return globalCoordinatesList;
    }

    public static double getDistanceMeter(GlobalCoordinates from, GlobalCoordinates to, Ellipsoid ellipsoid) {
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, from, to);
        return geoCurve.getEllipsoidalDistance();
    }

    public static List<Double> getDistanceMeter(GlobalCoordinates gpsFrom, List<GlobalCoordinates> gpsTo, Ellipsoid ellipsoid) {
        GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
        List<Double> distances = gpsTo.stream().map(to -> {
            return geodeticCalculator.calculateGeodeticCurve(ellipsoid, gpsFrom, to).getEllipsoidalDistance();
        }).collect(Collectors.toList());
        return distances;
    }
}
